package com.hsp.Demo_03_object;

public class Demo_17 {
    public static void main(String[] args) {
        /*
        6、编程创建一个Cale计算类，在其中定义2个变量表示两个操作数，定义四个方法实现求和、差、乘、商
           （要求除数为0的话，要提示）并创建两个对象，分别测试。
         */

        Cale cale1 = new Cale(3.0, 2.1);
        Cale cale2 = new Cale(6.0, 3.0);

        System.out.println("cale1中" + cale1.num1 + " + " + cale1.num2 + "=" + cale1.add());
        System.out.println("cale2中" + cale2.num1 + " + " + cale2.num2 + "=" + cale2.add());

        System.out.println("cale1中" + cale1.num1 + " - " + cale1.num2 + "=" + cale1.reduction());
        System.out.println("cale2中" + cale2.num1 + " - " + cale2.num2 + "=" + cale2.reduction());

        System.out.println("cale1中" + cale1.num1 + " * " + cale1.num2 + "=" + cale1.take());
        System.out.println("cale2中" + cale2.num1 + " * " + cale2.num2 + "=" + cale2.take());

        System.out.println("cale1中" + cale1.num1 + " / " + cale1.num2 + "=" + cale1.addition());
        System.out.println("cale2中" + cale2.num1 + " / " + cale2.num2 + "=" + cale2.addition());
    }
}

class Cale{
    double num1,num2;

    public Cale(){};
    public Cale(double num1, double num2){
        this.num1 = num1;
        this.num2 = num2;
    }

    /**
     * 实现两个变量相加
     */
    public double add(){
        return this.num1 + this.num2;
    }

    /**
     * 实现两个变量相减
     */
    public double reduction(){
        return this.num1 - this.num2;
    }

    /**
     * 实现两个变量相乘
     */
    public double take(){
        return this.num1 * this.num2;
    }

    /**
     * 实现两个变量相除
     * 由于如果直接返回一个 double 类型的 -1， 再通过后续判断，则会出问题，因为两数相除，有可能得到-1
     * 所以此处需要返回一个对象，通过对象的值来判断，结果
     * 如果返回的是null，则说明除数不能为0
     */
    public Double addition(){
       /* if(this.num2 == 0) {
            return -1;
        }
        return this.num1 / this.num2;*/

        return this.num2 == 0 ? null : this.num1 / this.num2;
    }
}
